package edu.faytechcc.csc.gui.search;

import edu.faytechcc.csc.messages.NetworkConnection;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

/**
 * This class is made to create a random series of five buttons linked to a
 * database. Sending a String to be sent through the NetworkConnection.
 *
 * @author Stephanie Mckenzie and Jasmin Pastari
 */
public class TopFivePanel extends JPanel
{

    private NetworkConnection conn = null;
    private SearchFrame searching;

    /**
     * Constructor that accepts a SearchFrame and sends five Strings to the
     * connection in the SearchFrame.
     *
     * @param search
     */
    public TopFivePanel(SearchFrame search)
    {
        this.setPreferredSize(new Dimension(300, 200));
        this.searching = search;
        this.conn = searching.getConn();
        this.setLayout(new GridLayout(5, 1));
        searching.setRandom(true);
        for (int index = 0; index < 5; index++)
        {
            conn.sendMessages("SELECT ENG_TITLE, PHOTO FROM Anime_Table"
                    + "ORDER BY RANDOM()");
        }
    }

    /**
     * Creates a button in this panel
     *
     * @param title
     * @param photo
     */
    public void createButton(String title, String photo)
    {
        JButton randomButton = new JButton(title);
        randomButton.addActionListener(new RandomActionListener());
        ImageIcon topFiveImage = new ImageIcon(photo);
        randomButton.setIcon(topFiveImage);
        this.add(randomButton);
    }

    /**
     *
     */
    private class RandomActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            String title = e.getActionCommand();
            conn.sendMessages("SELECT * FROM Anime_Table"
                    + " WHERE ENG_TITLE = '" + title + "'");

        }
    }
}
